class Node(object):
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children if children is not None else []


class Solution(object):
    def diameter(self, root):
        self.max_val = 0
        if not root:
            return 0
        def dfs(node, val):
            ans = []
            for child in node.children:
                ans.append(dfs(child, val + 1))
            ans.sort(reverse=True)
            if len(ans) >= 2:
                self.max_val = max(self.max_val, ans[0] + ans[1])
            if ans:
                now_max = ans[0]
            else:
                now_max = 0
            self.max_val = max(self.max_val, now_max + val)
            return now_max + 1

        dfs(root, 0)
        return self.max_val